package com.proj.mhl.view;

import com.proj.mhl.domain.*;
import com.proj.mhl.service.BillService;
import com.proj.mhl.service.DiningTableService;
import com.proj.mhl.service.EmployeeService;
import com.proj.mhl.service.MenuService;
import com.proj.mhl.utils.Utility;

import java.util.List;

/*
 * 满汉楼主界面
 * */
public class MHLView {
    //控制菜单
    private boolean loop = true;

    //接收用户选择
    private String key = "";

    //定义Service
    private EmployeeService employeeService = new EmployeeService();
    private DiningTableService diningTableService = new DiningTableService();
    private MenuService menuService = new MenuService();
    private BillService billService = new BillService();
    //显示所以餐桌状态
    public void TableState() {
        List<DiningTable> diningTables = diningTableService.selectAllTable();

        System.out.println("餐桌编号\t\t餐桌状态");
        for (DiningTable diningTable : diningTables) {

            System.out.println(diningTable.getId() + "\t\t\t" + diningTable.getState());
        }
        System.out.println("================显示完毕====================");
    }

    //预定餐桌
    public void OrderTable() {

        System.out.println("===================预定餐桌=================");
        System.out.println("请选择要预定餐桌编号(-1退出)：");
        Integer id = Utility.readInt();
        if (id == -1) {//判断输入餐桌是否存在
            System.out.println("=================取消预定餐桌=================");
            return;
        }
        System.out.print("确认是否预订(Y/N): ");
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {
            //根据读入的餐桌id，判断是否存在该餐桌
            DiningTable diningTable = diningTableService.selectSingleTab(id);
            if (diningTable == null) {
                System.out.println("餐桌不存在！");
                return;
            }
            //确定餐桌状态，是否已被预定
            if (!("空".equals(diningTable.getState()))) {
                System.out.println("餐桌已预定！");
                return;
            }
            //更新餐桌状态
            System.out.print("预定人姓名:");
            String name = Utility.readString(30);
            System.out.print("预定人电话:");
            String Tel = Utility.readString(30);

            if (diningTableService.orderTable(id, name, Tel)) {
                System.out.println("==========预定成功==========");
            } else {
                System.out.println("==========预定失败==========");
            }
        } else {
            System.out.println("=================取消预定餐桌=================");
        }
    }

    //查看菜单
    public void ViewMenu() {
        List<Menu> menus = menuService.selectMenu();
        System.out.println("菜品编号\t\t菜品名\t\t\t类别\t\t\t价格");
        for (Menu menu : menus) {
            System.out.println(menu.getId() + "\t\t\t" + menu.getName() + "\t\t\t" + menu.getType() + "\t\t\t" + menu.getPrice());

        }
        System.out.println("================显示完毕====================");
    }
    //点餐
    public void OrderMenu(){
        System.out.println("===================点餐服务=================");
        System.out.println("请输入点餐的桌号(-1退出)：");
        int orderDiningTableId = Utility.readInt();
        if(orderDiningTableId == -1){
            System.out.println("===================取消点餐=================");
            return;
        }
        System.out.println("请输入点餐的菜品号(-1退出)：");
        int orderMenuId = Utility.readInt();
        if(orderMenuId == -1){
            System.out.println("===================取消点餐=================");
            return;
        }
        System.out.println("请输入点餐的菜品量(-1退出)：");
        int orderNums = Utility.readInt();
        if(orderNums == -1){
            System.out.println("===================取消点餐=================");
            return;
        }
        //校验 餐桌，菜品是否存在
        //校验餐桌
        DiningTable diningTable = diningTableService.selectSingleTab(orderDiningTableId);
        if (diningTable == null) {
            System.out.println("餐桌不存在！");
            return;
        }
        //校验菜品
        Menu menu = menuService.selectSingleMenu(orderMenuId);
        if (menu == null) {
            System.out.println("菜品不存在！");
            return;
        }
        //点餐业务
        if (billService.OrderMenu(orderMenuId, orderNums, orderDiningTableId)) {
            System.out.println("==========下单成功==========");
        } else {
            System.out.println("==========下单失败==========");
        }

    }
    //显示所有账单
    public void ListView(){
        List<MultiTableBean> list = billService.list();
        System.out.println("编号\t\t菜品号\t\t菜品名\t\t菜品单价\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
        for (MultiTableBean bill :list) {
            System.out.println(bill.getId()+ "\t\t" +bill.getMenuId()+ "\t\t\t" +bill.getName()+ "\t\t" +bill.getPrice()+ "\t\t" +bill.getNums()+
                    "\t\t\t" +bill.getMoney()+ "\t\t" +bill.getDiningTableId()+ "\t\t" +bill.getBillDate()+
                    "\t\t" +bill.getState());
        }
        System.out.println("================显示完毕====================");

    }
    //结账
    public void PayBill(){
        System.out.println("===================结账服务=================");
        System.out.println("请选择要结账的餐桌编号(-1退出)：");
        int diningTableId = Utility.readInt();
        if(diningTableId == -1){
            System.out.println("===================取消结账=================");
            return;
        }
       if(diningTableService.selectSingleTab(diningTableId) == null){
           System.out.println("===================餐桌不存在=================");
           return;
       }
       if(!(billService.hasPayBillByDiningTableId(diningTableId))){
           System.out.println("===================餐桌无未结账账单=================");
           return;
       }
        System.out.println("结账的方式(现金/支付宝/微信)回车表示退出：");
        String payMode = Utility.readString(32,"");//如果回车键，返回默认值""
       if("".equals(payMode)){
           System.out.println("===================取消结账=================");
           return;
       }
        System.out.println("确认是否结账(Y/N):");
       char c = Utility.readConfirmSelection();
       if(c == 'Y'){
           if(billService.payBill(diningTableId,payMode)) {
               System.out.println("===================结账完成=================");
           }else{
               System.out.println("===================结账失败=================");
           }
       }else {
           System.out.println("===================取消结账=================");
       }
    }
    //main menu
    public void mainMenu() {


        while (loop) {
            System.out.println("===========满汉楼==============");
            System.out.println("\t\t1.登录满汉楼");
            System.out.println("\t\t1.退出满汉楼");
            System.out.print("请输入你的选择：");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.println("请输入员工号：");
                    String id = Utility.readString(50);
                    System.out.println("请输入密  码：");
                    String pwd = Utility.readString(50);
                    //数据库判断验证...
                    Employee employee = employeeService.getEmployeeByIdANDPwd(id, pwd);
                    if (employee != null) {
                        System.out.println("登录成功~" + employee.getName());
                        System.out.println("进入二级菜单");
                        System.out.println("===========满汉楼二级菜单==========");
                        while (loop) {
                            System.out.println("\t\t1 显示餐桌状态");
                            System.out.println("\t\t2 预定餐桌");
                            System.out.println("\t\t3 显示所有菜品");
                            System.out.println("\t\t4 点餐服务");
                            System.out.println("\t\t5 查看账单");
                            System.out.println("\t\t6 结账");
                            System.out.println("\t\t9 退出满汉楼");
                            System.out.print("请输入你的选择：");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    System.out.println("显示餐桌状态");
                                    TableState();
                                    break;
                                case "2":
                                    OrderTable();
                                    break;
                                case "3":
                                    System.out.println("显示所有菜品");
                                    ViewMenu();
                                    break;
                                case "4":
                                    System.out.println("点餐服务");
                                    OrderMenu();
                                    break;
                                case "5":
                                    System.out.println("查看账单");
                                    ListView();
                                    break;
                                case "6":
                                    System.out.println("结账");
                                    PayBill();
                                    break;
                                case "9":
                                    System.out.println("退出满汉楼");
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("输入有误，请重新输入");
                            }
                        }


                    } else {
                        System.out.println("登录失败");
                    }

                    break;
                case "2":
                    loop = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }
}
